package com.hulu.yec.teach.examplan.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hulu.yec.common.exception.DaoRuntimeException;
import com.hulu.yec.sys.user.entity.SysUserEntity;
import com.hulu.yec.teach.examplan.common.PageModel;
import com.hulu.yec.teach.examplan.entity.ExamPlanEntity;
import com.hulu.yec.teach.examplan.service.BackListDao;
import com.hulu.yec.teach.examplan.service.ExamPlanDao;

/**
 */
@WebServlet("/teach/examplan/ExamPlanServlet")
public class ExamPlanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int page = 1;
	private static final int pageSize = 5;

	/**
	 * get提交
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * post 提交
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf8");
		String command = request.getParameter("command");

		if ("list".equals(command)) {
			list(request, response);
		} else if ("examPlanDetail".equals(command)) {
			try {
				examPlanDetail(request, response);
			} catch (ClassNotFoundException e) {
				throw new DaoRuntimeException("补考人员查询错误！");
			}
		} else if ("explanadd".equals(command)) {
			// 值得回显
			explanadd(request, response);
		} else if ("explanAdd".equals(command)) {
			try {
				// 值的添加
				explanAdd(request, response);
			} catch (ClassNotFoundException e) {
				throw new DaoRuntimeException("数据库加载错误！");
			}
		} else if ("explanUpdate".equals(command)) {
			try {
				explanUpdate(request, response);
			} catch (ClassNotFoundException e) {
				throw new DaoRuntimeException("数据库加载错误！");
			}
		} else if ("UpdateState".equals(command)) {
			try {
				UpdateState(request, response);
			} catch (ClassNotFoundException e) {
				throw new DaoRuntimeException("数据库加载错误！");
			}
		} else {
			throw new ServletException("servlet连接错误！");
		}
	}

	/**
	 * 考试计划的修改
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	private void explanUpdate(HttpServletRequest request,
			HttpServletResponse response) throws IOException,
			ClassNotFoundException {
		// 获取前台参数
		String id = request.getParameter("id");
		String paperName = request.getParameter("paperName");
		String classroom = request.getParameter("classroom");
		String exam_Time_Start = request.getParameter("exam_Time_Start");
		String exam_Time_Stop = request.getParameter("exam_Time_Stop");
		String className = request.getParameter("className");
		// 获取Sesseion的id
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"user");
		int userId = user.getId();
		String state = request.getParameter("state");
		String memo = request.getParameter("memo");

		String sql = "UPDATE exam_plan SET exam_time_start='" + exam_Time_Start
				+ "',  exam_time_stop='" + exam_Time_Stop
				+ "', exam_classroom='" + classroom + "', exam_paper_id="
				+ paperName + ", to_user_ids='', to_class_id=" + className
				+ ", operate_user_id=" + userId
				+ ", operate_time= NOW(), state=" + state + ", memo='" + memo
				+ "' WHERE id=" + id;

		// 调用业务逻辑
		int addExamPlan = ExamPlanDao.AddExplan(sql);
		// 转向
		if (addExamPlan == 1) {
			response.sendRedirect("ExamPlanServlet?command=list&row="
					+ addExamPlan);
		} else {
			response.sendRedirect("/WEB-INF/view/teach/examplan/update-run.jsp");
		}
	}

	/**
	 * 状态的修改
	 * 
	 * @param request
	 * @param response
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	private void UpdateState(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			IOException {
		// 获取前台参数
		String id = request.getParameter("id");
		String state = request.getParameter("state");
		// 调用业务逻辑
		int stated = ExamPlanDao.SetateUpdate(state, id);
		// 转向
		response.sendRedirect("ExamPlanServlet?command=list&row=" + stated);
	}

	/**
	 * 添加考试计划
	 * 
	 * @param request
	 * @param response
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	private void explanAdd(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			IOException {
		// 获取前台参数
		String paperName = request.getParameter("paperName");
		String classroom = request.getParameter("classroom");
		String exam_Time_Start = request.getParameter("exam_Time_Start");
		String exam_Time_Stop = request.getParameter("exam_Time_Stop");
		String className = request.getParameter("className");
		// 获取Sesseion的id
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"user");
		int userId = user.getId();
		String state = request.getParameter("state");
		String memo = request.getParameter("memo");

		String sql = "INSERT INTO exam_plan VALUES(NULL, '" + exam_Time_Start
				+ "','" + exam_Time_Stop + "','" + classroom + "'," + paperName
				+ ",'" + 2 + "' ," + className + "," + userId + ", NOW(),"
				+ state + ", '" + memo + "')";
		// 调用业务逻辑
		int addExamPlan = ExamPlanDao.AddExplan(sql);
		// 转向
		if (addExamPlan == 1) {
			response.sendRedirect("ExamPlanServlet?command=list&row="
					+ addExamPlan);
		} else {
			response.sendRedirect("/WEB-INF/view/teach/examplan/update-run.jsp");
		}
	}

	/**
	 * 安排新增试卷、接收人员、接收班级的回显
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void explanadd(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取Sesseion的id
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"user");
		int userId = user.getId();
		// 调用业务逻辑
		List<ExamPlanEntity> paperList = BackListDao.PaperList();
		List<ExamPlanEntity> classList = BackListDao.ClassList();
		List<ExamPlanEntity> manger = BackListDao.MangerList(userId);
		String mangerName = manger.get(0).getName();
		// 转向
		request.setAttribute("mangerName", mangerName);
		request.setAttribute("paperList", paperList);
		request.setAttribute("classList", classList);
		request.getRequestDispatcher(
				"/WEB-INF/view/teach/examplan/examplan-add.jsp").forward(
				request, response);
	}

	/**
	 * 考试安排的分页查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ExamPlanEntity eplanList = new ExamPlanEntity();
		// 获取session的id
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"user");
		int userId = user.getId();

		// 获取操作的参数(新增和修改受音响行数以及状态的切换stated)
		int row = 0;
		if (!"".equals(request.getParameter("row"))
				&& request.getParameter("row") != null) {
			row = Integer.parseInt(request.getParameter("row"));
		}

		int stated = 0;
		if (!"".equals(request.getParameter("stated"))
				&& request.getParameter("stated") != null) {
			stated = Integer.parseInt(request.getParameter("stated"));
		}
		// 获取前台条件参数
		String coursename = null;
		if (request.getParameter("coursename") != null) {
			coursename = request.getParameter("coursename");
		}
		int ifmyupdte = 0;
		if (request.getParameter("ifmyupdte") != null || row == 1) {
			if (row == 1) {
				ifmyupdte = 1;
			} else {
				ifmyupdte = Integer.parseInt(request.getParameter("ifmyupdte"));
			}
		}
		int state = 0;
		if (request.getParameter("state") != null || stated == 1) {
			if (stated == 1) {
				state = 1;
			} else {
				state = Integer.parseInt(request.getParameter("state"));
			}
		}
		int timed = 0;
		if (request.getParameter("timed") != null) {
			timed = Integer.parseInt(request.getParameter("timed"));
		}

		int pageNo = page;
		if (request.getParameter("pageNo") != null
				&& Integer.parseInt(request.getParameter("pageNo")) > 1) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}

		// 调用业务
		PageModel<ExamPlanEntity> pageModel = ExamPlanDao.list(eplanList,
				pageNo, pageSize, state, timed, coursename, ifmyupdte, userId);
		// 转向
		if (userId != 0) {
			request.setAttribute("userId", userId);
		} else {
			request.setAttribute("userId", 3);
		}
		// 判断新增或修改成功与否

		request.setAttribute("ifmyupdte", ifmyupdte);

		request.setAttribute("pageModel", pageModel);
		request.setAttribute("eplanList", eplanList);
		request.setAttribute("coursename", coursename);
		request.setAttribute("state", state);
		request.setAttribute("timed", timed);
		request.getRequestDispatcher(
				"/WEB-INF/view/teach/examplan/examplan-list.jsp").forward(
				request, response);

	}

	/**
	 * 详情查询
	 * 
	 * @param request
	 * @param response
	 * @throws ClassNotFoundException
	 * @throws IOException
	 * @throws ServletException
	 */
	private void examPlanDetail(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			ServletException, IOException {
		// 获取session的id
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"user");
		int userId = user.getId();
		// 获取前台参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 获取全部补考人员id
		List<ExamPlanEntity> usersId = ExamPlanDao.UsersId(id);
		String str = usersId.get(0).getTo_user_ids();
		String[] ids = str.split(",");
		// 调用业务逻辑
		List<ExamPlanEntity> explan = ExamPlanDao.DetailList(id);
		List<ExamPlanEntity> usersName = ExamPlanDao.UsersName(ids);
		// 调用回显修改
		List<ExamPlanEntity> paperList = BackListDao.PaperList();
		List<ExamPlanEntity> classList = BackListDao.ClassList();
		List<ExamPlanEntity> userID = BackListDao.UserId(id);
		// i位登录用户在exam_plan相对应的id
		int i = userID.get(0).getOperate_user_id();
		// 转向
		request.setAttribute("classList", classList);
		request.setAttribute("paperList", paperList);
		request.setAttribute("explan", explan);
		request.setAttribute("usersName", usersName);
		if (userId == i) {
			request.getRequestDispatcher(
					"/WEB-INF/view/teach/examplan/detail-list.jsp").forward(
					request, response);
		} else {
			request.getRequestDispatcher(
					"/WEB-INF/view/teach/examplan/detail-lists.jsp").forward(
					request, response);
		}
	}
}
